<?php
namespace app\index\model;
use think\Model;
use think\Db;
use think\Cache;

class KnowUserVip extends Model
{
	public function getUserData($param = [], $userId = '')
	{
		if (!$userId) 
		{
			$user = user();
			$userId = $user['id'];
		}

		return self::field('user_id,nav_id,type_id,is_ques,expired_time,action')
					->where('valid', 1)
					->where('user_id', $userId)
					->when($param['nav_id'], function($query) use ($param){
						$query->where('nav_id', $param['nav_id']);
					})
					->order('expired_time desc')
					->select();
	}

	public function getAllDataIds($param = [], $field = 'nav_id', $userId = '' )
	{
		if (!$userId) 
		{
			$user = user();
			$userId = $user['id'];
		}

		return self::when($param['nav_id'], function($query)use ($param){
						$query->where('nav_id', 'in', $param['nav_id']);
					})
					->when($param['action'], function($query)use ($param){
						$query->where('action', $param['action']);
					})
					->where('valid', 1)
					->where('user_id', $userId)
					->column($field);
	}


	public function setUserVip($param, $status = 'admin')
	{
		if ($param['nav_id']){
			$userVips = self::where(['user_id'=>$param['id'],'valid'=>1])->column('nav_id');
			if ($status == 'admin')
			{
				$delVipId = array_diff($userVips,$param['nav_id']);
				if($delVipId) 
					self::where(['user_id'=>$param['id'],'nav_id'=>['in',implode(',', $delVipId)]])->delete();
			}
			$param['nav_id'] = is_array($param['nav_id']) ? $param['nav_id']: [$param['nav_id']];
			$param['money'] = is_array($param['money']) ? $param['money']: [$param['money']];
			$param['pack_id'] = is_array($param['pack_id']) ? $param['pack_id']: [$param['pack_id']];
			$arr = [];
			foreach($param['nav_id'] as $key=>$nav){
				$pack = Package::getOneData($param['pack_id'][$key]);
				$typeIds = $param['type_id'][$nav] ? implode(',', $param['type_id'][$nav]) : '';
				if(!in_array($nav, $userVips)){
					$arr[] = [
						'valid' => 1,
						'action' => 2,
						'nav_id' => $nav,
						'type_id' => $typeIds,
						'pack_id' => $param['pack_id'][$key],
						'is_ques' => $pack['is_ques'],
						'money' => $param['money'][$key] ?: 0,
						'order_id' => $param['orderid'] ?: 1,
						'user_id' => $param['id'],
						'updated_time' => date('Y-m-d H:i:s'),
						'created_time' => date('Y-m-d H:i:s'),
						'expired_time' => date('Y-m-d H:i:s',strtotime("+".($param['year'] ?: 2)." year")),
					];
				}else{
					$field = $status == 'admin' ? 'created_time' : 'expired_time';
					$userVip = self::where(['nav_id'=>$nav,'user_id'=>$param['id']])
									->where('valid', 1)->find();
					if ($status == 'index' || ($status == 'admin' && ($param['money'][$key] != $userVip['money'] || $userVip['type_id'] != $typeIds))){
						self::where(['nav_id'=>$nav,'user_id'=>$param['id']])->update([
							'type_id' => $typeIds,
							'money' => $param['money'][$key],
							'pack_id' => $param['pack_id'][$key],
							'is_ques' => $pack['is_ques'],
							'order_id' => $param['orderid'] ?: 1,
							'updated_time' => date('Y-m-d H:i:s'),
							'expired_time' => ['exp','DATE_ADD('.$field.', INTERVAL '.($param['year'] ?: 2).' YEAR)'],
						]);
					}
				}
			}
			$navModel = new KnowUserVip;
			if($arr) $navModel->saveAll($arr);
		}else{
			self::where('user_id',$param['id'])
					->where('action', 2)->update(['valid'=>0]);
		}
	}
}